package com.tac.service.scm.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tac.common.result.Result;
import com.tac.dao.scm.PurchasePlanDao;
import com.tac.dao.scm.PurchasePlanDetailsDao;
import com.tac.pojo.scm.PurchasePlan;
import com.tac.pojo.scm.PurchasePlanDetails;
import com.tac.service.scm.PurchasePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class PurchasePlanServiceImpl implements PurchasePlanService {

    @Autowired
    private PurchasePlanDao purchasePlanDao;

    @Autowired
    private PurchasePlanDetailsDao purchasePlanDetailsDao;
    //查看采购计划列表
    public Result<PageInfo<PurchasePlan>> getList(Integer pageNum,Integer pageSize,String documentNumber, String applicant, Date applicationDateStart, Date applicationDateEnd
            , String applicationStatus, String reviewer, Date reviewDateStart, Date reviewDateEnd, String reviewStatus){
        PageHelper.startPage(pageNum,pageSize);
        List<PurchasePlan> list = purchasePlanDao.getList(documentNumber, applicant, applicationDateStart, applicationDateEnd, applicationStatus, reviewer, reviewDateStart, reviewDateEnd, reviewStatus);
        PageInfo<PurchasePlan> purchasePlanPageInfo = new PageInfo<>(list);
        return Result.success(purchasePlanPageInfo);
    }

    //修改采购计划
    public Result update(PurchasePlan purchasePlan){
        PurchasePlan byId = purchasePlanDao.getById(purchasePlan.getId());
        if(purchasePlan.getApplicationStatus().equals("未提交")){
            if(byId.getApplicationStatus().equals("待审核")){
                return Result.error("已提交无法撤回");
            }
        }
        if(byId.getApplicationStatus().equals("已审核")){
            return Result.error("已审核状态无法修改采购计划");
        }
        //判断申请状态
        if(purchasePlan.getApplicationStatus().equals("已审核")){
            return Result.error("没有权限");
        }
        if(purchasePlan.getApplicationStatus().equals("待审核")){
            //申请日期
            purchasePlan.setApplicationDate(new Date());
        }
        Integer integer = purchasePlanDao.update(purchasePlan);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("修改采购计划失败");
    }

    //删除采购计划
    public Result remove(List<Integer> ids){
        for(Integer id:ids){
            PurchasePlan byId = purchasePlanDao.getById(id);
            if(!byId.getApplicationStatus().equals("未提交")){
                return Result.error("未提交状态才能删除");
            }
            //采购计划详情删除,不要变成垃圾占数据库
            purchasePlanDetailsDao.removeByPurchasePlanId(id);
        }
        Integer integer = purchasePlanDao.remove(ids);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("删除采购计划失败");
    }

    //添加采购计划
    public Result save(PurchasePlan purchasePlan){
        //不要让用户设置时间
        purchasePlan.setApplicationDate(null);
        //判断申请状态
        if(purchasePlan.getApplicationStatus().equals("已审核")){
            return Result.error("没有权限");
        }
        if(purchasePlan.getApplicationStatus().equals("待审核")){
            //申请日期
            purchasePlan.setApplicationDate(new Date());
        }
        //生成单据号
        String replace = UUID.randomUUID().toString().replace("-", "");
        purchasePlan.setDocumentNumber(replace);
        //添加假申请人、
        purchasePlan.setApplicant("童岸尘");
        Integer integer = purchasePlanDao.save(purchasePlan);
        List<PurchasePlanDetails> purchasePlanDetails = purchasePlan.getPurchasePlanDetails();
        //添加采购计划详情列表
        for(PurchasePlanDetails p:purchasePlanDetails){
            p.setPurchasePlanId(purchasePlan.getId());
            Integer save = purchasePlanDetailsDao.save(p);
            if(save<=0){
                return Result.error("添加采购计划详情失败");
            }
        }
        if(integer>0){
            PurchasePlan byId = purchasePlanDao.getById(purchasePlan.getId());
            return Result.success(byId);
        }
        return Result.error("添加采购计划失败");
    }

    //采购计划审核
    public Result updateByReview(PurchasePlan purchasePlan){
        //判断申请状态
        if(purchasePlanDao.getById(purchasePlan.getId()).getApplicationStatus().equals("未提交")){
            return Result.error("未提交状态不能审核");
        }
        //修改申请状态已审核
        purchasePlan.setApplicationStatus("已审核");
        //生成审核日期
        purchasePlan.setReviewDate(new Date());
        //生成假的审核人
        purchasePlan.setReviewer("管理员");
        Integer integer = purchasePlanDao.updateByReview(purchasePlan);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("采购计划审核失败");
    }

    //采购计划详情删除
    public Result remove2(List<Integer> ids){
        //判断采购计划的申请状态看能不能删除
        for(Integer id:ids){
            PurchasePlanDetails byId = purchasePlanDetailsDao.getById(id);
            PurchasePlan purchasePlan = purchasePlanDao.getById(byId.getPurchasePlanId());
            if(!purchasePlan.getApplicationStatus().equals("未提交")){
                return Result.error("只有未提交状态才能删除");
            }
        }
        Integer integer = purchasePlanDetailsDao.remove(ids);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("采购计划详情删除失败");
    }
    //采购计划详情修改
    public Result update2(PurchasePlanDetails purchasePlanDetails){
        PurchasePlan byId = purchasePlanDao.getById(purchasePlanDetailsDao.getById(purchasePlanDetails.getId()).getPurchasePlanId());
        if(!byId.getApplicationStatus().equals("未提交")){
            return Result.error("只有未提交状态能修改");
        }
        Integer integer = purchasePlanDetailsDao.update(purchasePlanDetails);
        if(integer>0){
            return Result.success(null);
        }
        return Result.error("采购计划详情修改失败");
    }
    //采购计划详情添加
    public Result save2(PurchasePlanDetails purchasePlanDetails){
        PurchasePlan byId = purchasePlanDao.getById(purchasePlanDetails.getPurchasePlanId());
        if(!byId.getApplicationStatus().equals("未提交")){
            return Result.error("只有未提交状态能添加");
        }
        Integer integer = purchasePlanDetailsDao.save(purchasePlanDetails);
        if(integer>0){
            return Result.success(purchasePlanDetails);
        }
        return Result.error("采购计划详情添加失败");
    }
}
